//
//  USMemorialDayModel.m
//  us
//
//  Created by lhx on 2025/4/29.
//

#import "USMemorialDayModel.h"

@implementation USMemorialDayModel

/// 日期描述
- (NSString *)dayDescription{
    if (self.anniversaryDate.length > 0) {
        if (self.repeatYearly) {
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:[self.anniversaryDate integerValue]];
            NSString *dateString = [date jk_stringWithFormat:@"M月d日"];
            return [NSString stringWithFormat:@"每年%@",dateString];
        }else{
            return [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:[self.anniversaryDate integerValue]] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
        }
    }
    return @"";

}
/// 周几
- (NSString *)weekDay {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[self.anniversaryDate integerValue]];
    NSInteger weekDay = [date jk_weekday];
    NSArray *weekArray = @[@"Sun.", @"Mon.", @"Tues.", @"Wed.", @"Thu.", @"Fri.", @"Sat."];
    if (weekArray.count > weekDay - 1) {
        return weekArray[weekDay-1];
    }
    return @"";
}

- (NSInteger)countdownToDate:(NSDate *)date {

    if (self.anniversaryDate.length == 0) {
        return 1;
    }
    NSCalendar *calendar = [NSCalendar currentCalendar];
 
    NSDate *today = [calendar startOfDayForDate:date];// 获取今天凌晨 0 点
    
    NSDate *anniversaryDate = [calendar startOfDayForDate:[NSDate dateWithTimeIntervalSince1970:[self.anniversaryDate integerValue]]]; // 当日凌晨 0 点

    if (self.repeatYearly) {
        // 获取原纪念日的月和日
           NSDateComponents *annivComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:anniversaryDate];
           NSDateComponents *todayComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
           
           // 构造今年的纪念日日期
           todayComponents.month = annivComponents.month;
           todayComponents.day = annivComponents.day;
           NSDate *thisYearAnniv = [calendar dateFromComponents:todayComponents];
           
           // 比较 today 和今年的纪念日
           NSComparisonResult result = [calendar compareDate:today toDate:thisYearAnniv toUnitGranularity:NSCalendarUnitDay];
           
           if (result == NSOrderedSame) {
               return 0; // 今天就是纪念日
           } else if (result == NSOrderedAscending) {
               // 今天在纪念日前
               NSDateComponents *diff = [calendar components:NSCalendarUnitDay fromDate:today toDate:thisYearAnniv options:0];
               return diff.day;
           } else {
               // 今天已过今年纪念日，算明年的
               todayComponents.year += 1;
               NSDate *nextYearAnniv = [calendar dateFromComponents:todayComponents];
               NSDateComponents *diff = [calendar components:NSCalendarUnitDay fromDate:today toDate:nextYearAnniv options:0];

               return diff.day;
           }
    } else {
        NSInteger days = [today jk_distanceDaysToDate:anniversaryDate];
        return days;
       
    }
}
/// 天数
- (NSInteger)dayCount {
 
    NSInteger count = [self countdownToDate:[NSDate date]];
    if (count == 0) {
        return 0;
    }
    if (self.repeatYearly) {
        return count;
    }else{
        if (self.includeStart) {
            if (count < 0) {
                return count - 1;
            }else{
                return count + 1;
            }
        }else{
            return count;
        }
    }
  
}

- (NSMutableAttributedString *)dayAttributeWithFontSize:(NSInteger)fontSize{
    NSMutableAttributedString *text;
    
    
    UIColor *color = [UIColor jk_colorWithHexString:self.color];
    NSInteger count = labs(self.dayCount);
    if (self.dayCount < 0 || (self.dayCount == 0 && self.togetherFlag && self.includeStart)) {
        if (count == 0) {
            count = 1;//在一起今天且算起始日
        }
        text = [[NSMutableAttributedString alloc]initWithString:@"Day+\n"attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    }else if (self.dayCount == 0 && (self.repeatYearly || self.togetherFlag)){//重复的显示今天
        text = [[NSMutableAttributedString alloc]initWithString:@"今天" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize weight:UIFontWeightBold],NSForegroundColorAttributeName:color}];
        return text;
    }else{
        text = [[NSMutableAttributedString alloc]initWithString:@"还有"attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    }
    NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%li",count] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize weight:UIFontWeightBold],NSForegroundColorAttributeName:color}];
    
    NSMutableAttributedString *text2 = [[NSMutableAttributedString alloc]initWithString:@"天"attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    [text appendLocalizedFormat:text1];
    [text appendLocalizedFormat:text2];
    
    
    return text;
    
}

@end

@implementation USMemorialDaySuperModel

+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"noRepeatYearlylist": [USMemorialDayModel class],
        @"repeatYearlylist": [USMemorialDayModel class]
    };
}

@end
